package util.parser;

/**
 * 算符优先模块
 */
public class OperatorPriority {
    private OperatorPriority() {
    }

    public static int getPriority(String tok) {
        if (isNumber(tok)) return 5;
        if (tok.equals("/") || tok.equals("*"))
            return 3;
        if (tok.equals("-") || tok.equals("+"))
            return 1;
        return 0;
    }

    //偏序<矩阵 主要用作二叉树设置左子树判定的问题
    public static boolean isLower(String left, String root) {
        int px = getPriority(left);
        int py = getPriority(root);
        return px < py;

    }

    //偏序>矩阵 主要用作二叉树设置右子树判定的问题
    public static boolean isHigher(String root, String right) {
        int px = getPriority(root);
        int py = getPriority(right);
        if (px != py) return px > py;
        return root.equals("-") || root.equals("/");
    }

    //+12.34 -1e4 1.2f 2. 8. .5 1.2E-3
    public static boolean isNumber(String tok) {
        try {
            Double.parseDouble(tok);
            return true;

        } catch (NumberFormatException e) {
            return false;
        }
    }

}
